package com.lenovo.uitest;

import java.io.IOException;

import android.graphics.Rect;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;

import com.android.uiautomator.core.UiCollection;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

public class Camera2 extends UiAutomatorTestCase {

	public static final String CAMERA_PACKAGENAME_SUPER="com.lenovo.scg";
	public static final String CAMERA_ACTIVITY_SUPER="com.android.camera.Camera";
	public static final String CAMERA_PACKAGENAME_LITE="com.lenovo.scgmtk";
	public static final String CAMERA_ACTIVITY_LITE="com.android.camera.CameraLauncher";
	public static final String CAMERA_ACTIVITY_VIDEO="com.android.camera.VideoCamera";
	
	private String shutterBtnDesc=".*快门.*|Shutter button";
	
	UiBase uiBase=new UiBase();
	
	//启动录像机
	public void testLaunchCamera() throws UiObjectNotFoundException, IOException {
		if(!launchAppForExec(CAMERA_PACKAGENAME_SUPER, CAMERA_ACTIVITY_VIDEO)){
			launchAppForExec(CAMERA_PACKAGENAME_LITE, CAMERA_ACTIVITY_LITE);
		}
	}
	//点击录像按钮开始录像
	public void testStartVideoback1() throws UiObjectNotFoundException, IOException {
		int X=device().getDisplayWidth();
		int Y=device().getDisplayHeight();
		device().click(X/2+80, Y-80);
	}
	public void testStartVideo() throws UiObjectNotFoundException, IOException {
					
			//快门
			UiCollection start_video=collection(selector().textMatches("android.widget.FrameLayout"));			
			UiObject start1=start_video.getChildByInstance(selector().descriptionMatches(".*快门.*|Shutter button"), 0);
			UiObject start2=start_video.getChildByInstance(selector().descriptionMatches(".*快门.*|Shutter button"), 1);	
			UiObject video=object(selector().descriptionMatches(shutterBtnDesc));
			if (start2.exists()) {
				Rect rect2=start2.getBounds();
				device().click(rect2.centerX(), rect2.centerY());
				Log.v("tag", "dddd:1111");
			}else if(start1.exists()){
				Rect rect1=start1.getBounds();
				device().click(rect1.centerX(), rect1.centerY());
				Log.v("tag", "dddd:2222");
			}else if (video.exists()) {
				video.click();
			}	
			else {
				int X=device().getDisplayWidth();
				int Y=device().getDisplayHeight();
				device().click(X/2+80, Y-80);
				Log.v("tag", "dddd:3333");
			}
	}
	//结束录像
	public void testCloseVideo() throws UiObjectNotFoundException {
		UiObject video1=object(selector().descriptionMatches(shutterBtnDesc));
		//UiObject video2=object(selector().className("android.widget.ImageView").focusable(true));
		if (video1.exists()) {
			video1.click();
		}else {
			UiObject end_videoObject=object(selector().className("android.widget.FrameLayout").index(3))
					.getChild(selector().className("android.widget.LinearLayout").index(2))
					.getChild(selector().className("android.widget.FrameLayout"));
			end_videoObject.click();
		}
		
		
		
	}
	//回到主屏幕，清除后台
	public void testClearBg() throws RemoteException, UiObjectNotFoundException {
		device().pressHome();
		device().pressRecentApps();
		object(selector().className("android.widget.Button")).click();
		sleep(2000);
	}
	//唤醒解锁
	public void testUnlock() throws RemoteException {
		if (!device().isScreenOn()) {
			device().wakeUp();	
			uiBase.UnLock();
		}
	}
	//灭屏
	public void testSleep() throws RemoteException {
		device().sleep();
	}
	
	
	
	public boolean launchAppForExec(String packagename, String activity)
			throws UiObjectNotFoundException, IOException {
		String shellcmd = "am start -n " + packagename + "/" + activity;		
		int i=0;
	    while (!packagename.equals(device().getCurrentPackageName())) {
	    	Runtime.getRuntime().exec(shellcmd);
	    	if(i++>=15)break;			
		}		
		return device().getCurrentPackageName().equals(packagename);
	}
	
	/*
	 * �����װ
	 */
	public UiDevice device() {
		return UiDevice.getInstance();	
	}
	public UiSelector selector() {
		return new UiSelector();
	}
	public UiObject object(UiSelector selector) {
		return new UiObject(selector);	
	}
	public UiCollection collection(UiSelector selector) {
		return new UiCollection(selector);
	}
	public UiScrollable scrollable(UiSelector selector) {
		return new UiScrollable(selector);	
	}

	/*
	 * uiselector������װ
	 * selector�࿪ʼѡ������һ��Ϊ���࣬����Ϊ��������
	 */
	public UiSelector text(String text) {
		return selector().textMatches(text);
	}
	public UiSelector desc(String desc) {
		return selector().descriptionMatches(desc);
	}
	public UiSelector classname(String className){
		return selector().classNameMatches(className);
	}
	
	
	public void UnLock(){
		String lockPackageName="com.lenovo.lockscreen.theme.davinci";
		String lock="com.lenovo.lockscreen.theme.lessismore";
		int X=device().getDisplayWidth();
		int Y=device().getDisplayHeight();
		int i=0;
		String current=device().getCurrentPackageName();
		
		
		UiObject imageObject1=object(selector().className("android.widget.ImageView"));
		Rect rect1;
		try {
			rect1 = imageObject1.getBounds();
		
		int x11=rect1.centerX();
		int x21=rect1.centerY();
		
		device().swipe(x11, x21, x11, Y-10, 10);
		} catch (UiObjectNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		while(lockPackageName.equals(current)){
			device().swipe(X/2, Y-80, X/2, Y/4, 10);
			if (i++>=10) {
				break;
			}
		}
		
		/*
		if(lock.equals(device().getCurrentPackageName())){
			UiObject imageObject=object(selector().className("android.widget.ImageView"));
			Rect rect;
			try {
				rect = imageObject.getBounds();
		
			int x1=rect.centerX();
			int x2=rect.centerY();
			
			device().swipe(x1, x2, x1, Y-10, 10);
			} catch (UiObjectNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else {
			
		}
		*/
	}
	
	
	
}
